﻿using System.Windows.Controls;

namespace NavFx
{
	/// <summary>
	/// Most basic of Transitors that simply displays the specified next page using a target
	/// </summary>
	/// <remarks>
	/// This implementation of a Transitor only supports displaying the Next page, any Previous page
	/// is ignored.  It aslo is limited to Application or Panel as the type for Target.  If Target is
	/// set to Application the page is displayed as the RootVisual for the Silverlight host, that is it
	/// replaces everything.  If the Target is a Panel the page is set as the only child control.
	/// </remarks>
	public class SimpleTransitor: ITransitor
	{
		#region Private members

		private object target = null;

		#endregion

		#region Public members

		/// <summary>
		/// Target for displaying the page
		/// </summary>
		public object Target
		{
			get
			{
				return this.target;
			}

			set
			{
				if(value is Application || value is Panel || value is UserControl)
				{
					this.target = value;
				}
				else
				{
					throw (new TargetNotSupportedException("The object is not supported as a navigation target by the NavFx framework"));
				}
			}
		}
		#endregion

		#region ITransitor Members

		/// <summary>
		/// Change to the page specified as next
		/// </summary>
		/// <param name="previousPage">Ignored for this implemenation</param>
		/// <param name="nextPage">Target page to be displayed</param>
		public void TransitionPage(IPage previousPage, IPage nextPage)
		{

			if(this.Target == null)
			{
				//No target set to contain the page control
				throw (new TargetNotSetException());
			}

			if(nextPage is UserControl)
			{
				//get the usercontrol interface of the nextPage
				UserControl nextControl = nextPage as UserControl;
		
				if(this.Target is Application)
				{
					//Target is the Silverlight host application
					Application targetApp = (Application)this.Target;
					if(targetApp.RootVisual is IHostPage)
					{
						//replace the current display with the specified page
						((IHostPage)targetApp.RootVisual).SetContent(nextControl);
						targetApp.RootVisual.UpdateLayout();
					}
				}
				else
				{
					if(this.Target is IHostPage)
					{
						//Target supports the NavFx page interface
						//replace the content using the method provided
						((IHostPage)this.Target).SetContent(nextControl);
					}
					else
					{
						if(this.Target is Panel)
						{
							//the target is a Panel, clear any existing
							//child controls and replace them with the nextPage
							Panel targetPanel = (Panel)this.Target;

							targetPanel.Children.Clear();
							targetPanel.Children.Add(nextControl);
							targetPanel.UpdateLayout();
						}
					}
				}
			}
			else
			{
				throw (new PageTypeNotSupportedException("Only UserControl is supported as the base type for pages handled by the SimpleTransitor"));
			}
		}

		#endregion

		#region Constructors

		/// <summary>
		/// Default constructor
		/// </summary>
		public SimpleTransitor()
		{
			//nothing to do
		}

		/// <summary>
		/// Constructore overload supporting setting of the target
		/// </summary>
		/// <param name="target">Target container for displaying the next page</param>
		public SimpleTransitor(object target)
		{
			this.Target = target;
		}
		#endregion


	}
}
